/**
 * @file vector2.cpp
 *
 * @date Oct 20, 2009
 * @author Ives van der Flaas
 */

#include "vector2.h"
#include <cmath>
#include <iostream>


namespace si
{
	Vector2::Vector2():
		fx(0),fy(0)
	{

	}

	Vector2::Vector2(const double x, const double y):
		fx(x),fy(y)
	{

	}

	Vector2::Vector2(const Vector2& in)
	{
		// Don't use operator=; stack overflow's all the way
		// to china...
		fx = in.getX();
		fy = in.getY();
	}

	Vector2 Vector2::operator=(const Vector2& in)
	{
		fx = in.getX();
		fy = in.getY();

		return *this; // returns BY VALUE...
	}


    Vector2& Vector2::operator +=(const Vector2 & v)
    {
    	*this = *this + v;
    	return *this;
    }

	double Vector2::length() const
	{
		return sqrt(fx*fx + fy*fy);
	}

	Vector2 Vector2::operator-(const Vector2& in) const
	{
		return Vector2(fx - in.getX(), fy - in.getY());
	}

	Vector2 Vector2::operator+(const Vector2& in) const
	{
		return Vector2(fx + in.getX(), fy + in.getY());
	}

	Vector2 Vector2::operator+(const double& d) const
	{
		return Vector2(fx+d, fy+d);
	}

	Vector2 Vector2::operator-(const double& d) const
	{
		return Vector2(fx-d, fy-d);
	}

	Vector2 Vector2::operator*(const double& d) const
	{
		return Vector2(fx*d, fy*d);
	}

	Vector2 Vector2::operator/(const double& d) const
	{
		return Vector2(fx/d, fy/d);
	}

	Vector2 Vector2::operator-() const
	{
		return -1.0 * (*this);
	}

	Vector2 Vector2::operator+() const
	{
		return (*this);
	}

	Vector2 Vector2::operator/=(const double& d)
	{
		return *this = *this / d;
	}

	Vector2 Vector2::operator*=(const double& d)
	{
		return *this = *this * d;
	}

	Vector2 Vector2::operator+=(const double& d)
	{
		return *this = *this + d;
	}

	Vector2 Vector2::operator-=(const double& d)
	{
		return *this = *this - d;
	}

	bool Vector2::operator==(Vector2& v) const
	{
		if((fx == v.getX()) && (fy == v.getY()))
			return true;
		else
			return false;
	}

	bool Vector2::operator!=(Vector2& v) const
	{
		return !((*this) == v);
	}

	void Vector2::normalize()
	{
		*this /= length();
	}

	double Vector2::dotProduct(const Vector2& v2) const
	{
		return fx*v2.getX() + fy*v2.getY();
	}




	Vector2 operator*(const double& d, const Vector2& v)
	{
		return Vector2(d*v.getX(), d*v.getY());
	}

	std::ostream& operator<<(std::ostream& os, Vector2& v)
	{
		os << "x:" << v.getX() << "\ty:" << v.getY() << std::endl;
		return os;
	}
}
